package com.laboratory.temp;

import com.laboratory.util.FileUtil;

import java.io.*;
import java.util.*;

/**
 * Created by admin on 2019/04/13.
 */
public class SQLGenerate {

    static String  sourceFilePath = System.getProperty("user.dir") + "/src/test/resource/" + "UTF8.txt";

    /**
     * 洛阳-服务请求-生产-ngwf_ah-20190410-2-1-insert-sql.txt
     * 洛阳-服务请求-生产-ngwf_ah-20190410-2-1-insert-rollback-sql.txt
     */
    public static void generate(){
       List<List<String>>  list = getProvinceAndDBMapper();
       String sourceFilePath = "";
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFilePath), "UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                for (List<String> temp:list){
                    String provCode = temp.get(1);
                    String dbName = temp.get(4);
                    String IP = temp.get(2);
                    String PORT = temp.get(3);
                    String user = temp.get(5);

                    String lineWrite = line.replace("'00030016'",provCode);
                    String fileName = " 洛阳-服务请求-生产-"+ dbName +"-20190414-insert-sql.txt";
                    String filePath = "G:/GitRepository/util/src/main/java/com/laboratory/temp/sql/";
                    File file  = new File(filePath+fileName);
                    if (!file.exists()){
                        file.createNewFile();
                        FileUtil.appendLine(file, "/*","UTF-8");
                        FileUtil.appendLine(file, "*地址："+ IP,"UTF-8");
                        FileUtil.appendLine(file, "*端口号："+ PORT,"UTF-8");
                        FileUtil.appendLine(file, "*库名："+ dbName,"UTF-8");
                        FileUtil.appendLine(file, "*用户名："+ user,"UTF-8");
                        FileUtil.appendLine(file, "*操作数量：1","UTF-8");
                        FileUtil.appendLine(file, "*是否中间件：否","UTF-8");
                        FileUtil.appendLine(file, "*数据库类型：mysql","UTF-8");
                        FileUtil.appendLine(file, "*工单联系人电话: xx  xx","UTF-8");
                        FileUtil.appendLine(file, "*工单执行失败是否回滚:全部回滚","UTF-8");
                        FileUtil.appendLine(file, "*/" ,"UTF-8");
                    }
                    FileUtil.appendLine(file, lineWrite,"UTF-8"); //在文件末尾追加一行

                }
            }
        } catch (IOException e) {
        e.printStackTrace();
    }
    }
    /**
     * 洛阳-服务请求-生产-ngwf_ah-20190410-2-1-insert-sql.txt
     * 洛阳-服务请求-生产-ngwf_ah-20190410-2-1-insert-rollback-sql.txt
     */
    public static void generateRollback(){
        List<List<String>>  list = getProvinceAndDBMapper();
        String sourceFilePath = "";
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceFilePath), "UTF-8"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                for (List<String> temp:list){
                    String provCode = temp.get(1);
                    String dbName = temp.get(4);
                    String IP = temp.get(2);
                    String PORT = temp.get(3);
                    String user = temp.get(5);
                    String fileName = " 洛阳-服务请求-生产-"+ dbName +"-20190414-rollback-sql.txt";
                    String filePath = "G:/GitRepository/util/src/main/java/com/laboratory/temp/sql/";
                    File file  = new File(filePath+fileName);
                    if (!file.exists()){
                        file.createNewFile();
                        FileUtil.appendLine(file, "/*","UTF-8");
                        FileUtil.appendLine(file, "*地址："+ IP,"UTF-8");
                        FileUtil.appendLine(file, "*端口号："+ PORT,"UTF-8");
                        FileUtil.appendLine(file, "*库名："+ dbName,"UTF-8");
                        FileUtil.appendLine(file, "*用户名："+ user,"UTF-8");
                        FileUtil.appendLine(file, "*操作数量：1","UTF-8");
                        FileUtil.appendLine(file, "*是否中间件：否","UTF-8");
                        FileUtil.appendLine(file, "*数据库类型：mysql","UTF-8");
                        FileUtil.appendLine(file, "*工单联系人电话: xx  xx","UTF-8");
                        FileUtil.appendLine(file, "*工单执行失败是否回滚:全部回滚","UTF-8");
                        FileUtil.appendLine(file, "*/" ,"UTF-8");
                    }
                    FileUtil.appendLine(file, line,"UTF-8"); //在文件末尾追加一行

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public static  Map<String, List<String>> getDBIP(){
       String filePath =  "G:/GitRepository/util/src/main/java/com/laboratory/temp/DB IP";

        Map<String, List<String>> allDBIPMap = new HashMap<>();

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
            String line ;
            while ((line = reader.readLine()) != null){
                String[] cellArr = line.split("\t");
                String provName = cellArr[4];
                allDBIPMap.put(provName, Arrays.asList(cellArr));
            }
        }   catch (IOException e) {
            e.printStackTrace();
        }
        return allDBIPMap;
    }

    public static  Map<String, String> getProvName(){
        String filePath =  "G:/GitRepository/util/src/main/java/com/laboratory/temp/provName";

        Map<String, String> allProvNameMap = new HashMap<>();

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
            String line ;
            while ((line = reader.readLine()) != null){
                String[] cellArr = line.split("\t");
                String provName = cellArr[0];
                String provCode = cellArr[1];
                allProvNameMap.put(provName, provCode);
            }
        }   catch (IOException e) {
            e.printStackTrace();
        }
        return allProvNameMap;
    }

    public static void getProvinceAndDB(){
        Map<String, List<String>>  allDBIPMap = getDBIP();
        Map<String, String> provNameMap = getProvName();
        BufferedWriter bufferedWriter = null;
        try {
            FileWriter writer = new FileWriter("G:/GitRepository/util/src/main/java/com/laboratory/temp/provinceAndDB.txt");
              bufferedWriter = new BufferedWriter(writer);
            Iterator iter = allDBIPMap.entrySet().iterator();
            while (iter.hasNext()){
                Map.Entry entry = (Map.Entry) iter.next();
                String provName = (String)entry.getKey();
                List<String>  list = (List<String>)entry.getValue();
                String provCode = provNameMap.get(provName);

                String lineStr =provName +  "\t" + provCode + "\t" +  list.get(0) +"\t" + list.get(1) + "\t" + list.get(2) + "\t" +list.get(3);
                bufferedWriter.write(lineStr);
                bufferedWriter.newLine();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    public static  List<List<String>>  getProvinceAndDBMapper(){
        String filePath =  "G:/GitRepository/util/src/main/java/com/laboratory/temp/provinceAndDB.txt";
        List<List<String>> list = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
            String line ;
            while ((line = reader.readLine()) != null){
                String[] cellArr = line.split("\t");
                String provCode = cellArr[1];
                list.add(Arrays.asList(cellArr));

            }
        }   catch (IOException e) {
            e.printStackTrace();
        }
        return list;


    }

    public static void main(String[] args) {
        getDBIP();
        getProvName();
        getProvinceAndDBMapper();
    }
}
